/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.vntnumberpickerpreference;

import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Picker;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

/**
 * 文件描述
 *
 * @since 2021-05-21
 */
public class CustomPickerPopup extends CenterPopupView {
    private static final int FROMARGBINT = 0xffffffff;
    private static final int CORNERRADIUS = 10;
    private static final int WIDTH = 150;
    private Picker picker;
    private final ParameterEntity mParameterEntity;

    /**
     * getIntFromAttr
     *
     * @param parameterEntity 实体类
     */
    public CustomPickerPopup(ParameterEntity parameterEntity) {
        super(parameterEntity.getContext(), null);
        this.mParameterEntity = parameterEntity;
    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_custom_picker_popup;
    }

    // 执行初始化操作，比如：findComponentById，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        picker = (Picker) findComponentById(ResourceTable.Id_picker);
        DirectionalLayout dlDirectional = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_directional);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(FROMARGBINT));
        element.setCornerRadius(CORNERRADIUS);
        dlDirectional.setBackground(element);
        DependentLayout dlDependent = (DependentLayout) findComponentById(ResourceTable.Id_dl_dependent);
        dlDependent.setBackground(element);
        Text titleText = (Text) findComponentById(ResourceTable.Id_titleText);
        titleText.setText(mParameterEntity.getTitle());
        picker.setWheelModeEnabled(mParameterEntity.isWheelModeEnabled());
        picker.setMaxValue(mParameterEntity.getMaxValue());
        picker.setMinValue(mParameterEntity.getMinValue());
        picker.setValue(Integer.parseInt(PreferenceUtils.getString(getContext().getApplicationContext(),
            mParameterEntity.getKey(), mParameterEntity.getValue())));
        findComponentById(ResourceTable.Id_cancelText).setClickedListener(component -> {
            dismiss(); // 关闭弹窗
        });
        findComponentById(ResourceTable.Id_determineText).setClickedListener(component -> {
            PreferenceUtils.putString(getContext().getApplicationContext(),
                mParameterEntity.getKey(), String.valueOf(picker.getValue()));
            new ToastDialog(getContext())
                .setText("New value is " + picker.getValue())
                // Toast显示在界面中间
                .setAlignment(LayoutAlignment.BOTTOM)
                .show();
            dismiss(); // 关闭弹窗
        });
    }

    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    @Override
    protected int getMaxWidth() {
        return mParameterEntity.getAbilitySlice().getWindow().getLayoutConfig().get().width - WIDTH;
    }
}